<?php

namespace App\Controller\User;

use App\Controller\ApiController;
use Usap\Service\Service;

/**
 *
 * @desc 账户资料
 */
class AccountController extends ApiController
{


    /*
    *用户登录
    */
    public function login()
    {
//        \Logger::getLogger('home.user.account')->apps('login_start')->info(I());
        $data = I('request.');
        $userName = trim($data['userName']);
        $passWord = trim($data['passWord']);
        $login_type = $data['loginType'];//0-网站,1-客户端Android,2-客户端IOS
        if (empty($userName)) {
            $this->ajaxReturn(array('code' => -1001, 'code_str' => "请输入用户名!"));
        }
        if (empty($passWord)) {
            $this->ajaxReturn(array('code' => -1002, 'code_str' => "请输入密码!"));
        }
        $log_info = array();
        $log_info['action'] = 'begin';
        $log_info['request'] = I();
        \Logger::getLogger('app.user.account')->apps('login_start')->info($log_info);
        //实例化
        $user_service = Service::instance()->find('account', 'user');
        $login_result = $user_service->login($userName, md5($passWord), $login_type);
        $return = array('code' => '0000', 'msg' => '登录成功', 'data' => array('user_data' => $login_result['user_data']));
        \Logger::getLogger('app.admin.admin')->apps('login_end')->info($login_result);
        if ($login_result['code'] == '0000') {
            $this->ajaxReturn($return);
        } else {
            $this->ajaxReturn($login_result);
        }
    }

    /*
    *退出登录
    */
    public function logout()
    {

//        // 立即返回，不等待处理结果
        ob_start();
        header('Content-Type: application/json');
        echo json_encode(['code' => '0000', 'msg' => '成功退出', 'data' => ['info' => []]]);
        ob_end_flush();
        $accessToken = I('request.access_token');
        $user_service = Service::instance()->find('account', 'user');
        $user_service->logout($accessToken);


    }

    /**
     * 修改密码
     */
    public function modifypwd()
    {

        $user_id = $this->context->userId;
        \Logger::getLogger('home.user.account')->apps('login_start')->info(I());
        if (empty($user_id)) {
            $this->ajaxReturn(array('code' => -1001, 'msg' => "请先登录"));
        }
        $request = I('request.');
        $oldPassWord = trim($request['oldPassWord']);
        $newPassWord = trim($request['newPassWord']);
        if (empty($oldPassWord)) {
            $data = array('code' => -10000, 'msg' => '输入原密码', 'data' => []);
        }
        if (empty($newPassWord)) {
            $data = array('code' => -10001, 'msg' => '请输入新密码', 'data' => []);
        }
        $user_service = Service::instance()->find('account', 'user');
        $modify_result = $user_service->modifyLoginPwd($user_id, $oldPassWord, $newPassWord);
        if ($modify_result['code'] != '0000') {
            //更新清除登录锁
            $user_service->cleanLoginLock($user_id);
            $this->ajaxReturn(array('code' => -1002, 'code_str' => $modify_result['msg'], 'info' => ''));
        }else{
            $data = array('code' => 0000, 'msg' => '修改成功,重新登陆', 'data' => ['info' => []]);
        }
        $accessToken = I('request.access_token');
        $user_service = Service::instance()->find('account', 'user');
        $user_service->logout($accessToken);
        $this->ajaxReturn($data);
    }
}
